<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
 * Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
 * RMH Homebase is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
*/

/**
 * Functions to create, update, and retrieve information from the
 * dbShifts table in the database.  This table is used with the Shift
 * class.  Shifts are generated using the master schedule (through the
 * addWeek.php form), and retrieved by the calendar form and editShift.
 * @version May 1, 2008
 * @author Maxwell Palmer
 */

include_once('Shift.php');
include_once('dbPersons.php');
include_once('dbSCL.php');
include_once('dbinfo.php');

/**
 * Drops the dbShifts table if it exists, and creates a new one
 * Table fields:
 * id: mm-dd-yy-ss-ss or mm-dd-yy-ss-ssFam or mm-dd-yy-chef
 * vacancies: # of vacancies for this shift
 * persons: list of people ids, followed by their name, ie "max1234567890+Max+Palmer"
 * sub_call_list: yes/no if shift has SCL
 * notes: shift notes
 */
function setup_dbShifts() {
   connect();
   mysql_query("DROP TABLE IF EXISTS dbShifts");
   $result=mysql_query("CREATE TABLE dbShifts (id CHAR(17) NOT NULL, vacancies INT, persons TEXT, sub_call_list TEXT, notes TEXT, PRIMARY KEY (id))");
	if(!$result) {
		echo mysql_error();
		return false;
	}
	mysql_close();
	return true;
}

/**
 * Inserts a shift into the db
 * @param $s the shift to insert
 */
function insert_dbShifts($s) {
   if (! $s instanceof Shift) {
		die ("Invalid argument for dbShifts->add_shift function call");
	}
   connect();
   $query = "SELECT * FROM dbShifts WHERE id =\"".$s->get_id()."\"";
    $result = mysql_query ($query);
    if (mysql_num_rows($result)!=0)
   		delete_dbShifts($s);
	$query="INSERT INTO dbShifts VALUES (\"".$s->get_id()."\",".$s->num_vacancies().",\"".
				implode("*",$s->get_persons())."\",\"".$s->get_sub_call_list()."\",\"".$s->get_notes()."\")";
	$result=mysql_query($query);
    if(!$result) {
		echo "unable to insert into dbShifts ".$s->get_id().mysql_error();
        mysql_close();
        return false;
    }
    mysql_close();
    return true;
 }

/**
 * Deletes a shift from the db
 * @param $s the shift to delete
 */
function delete_dbShifts($s) {
	if (! $s instanceof Shift)
		die ("Invalid argument for dbShifts->remove_shift function call");
	connect();
    $query="DELETE FROM dbShifts WHERE id=\"".$s->get_id()."\"";
	$result=mysql_query($query);
	if(!$result) {
		echo "unable to delete from dbShifts ".$s->get_id().mysql_error();
        mysql_close();
        return false;
	}
    mysql_close();
    return true;
 }

/**
 * Updates a shift in the db by deleting it (if it exists) and then replacing it
 * @param $s the shift to update
 */
function update_dbShifts($s) {
	if (! $s instanceof Shift)
		die ("Invalid argument for dbShifts->replace_shift function call");
	delete_dbShifts($s);
	insert_dbShifts($s);
	return true;
}

/**
 * Selects a shift from the database
 * @param $id a shift id
 * @return Shift or false
 */
function select_dbShifts($id) {
	connect();
	$s=null;
   $query = "SELECT * FROM dbShifts WHERE id =\"".$id."\"";
   $result = mysql_query ($query);
	if (!$result) {
	    echo 'Could not run query2: ' . mysql_error();
	}
	else {
		$result_row=mysql_fetch_row($result);
		mysql_close();
		if($result_row) {
			$s=new Shift($result_row[0], $result_row[1], explode("*",$result_row[2]), $result_row[3],$result_row[4]);
		}
	}
	return $s;
}

?>
